package com.example.shop_2210.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.databinding.ActivityPasswordBinding

class PasswordActivity : BaseActivity<ActivityPasswordBinding,LoginViewModel>() {
    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.passwordSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                val intent = Intent(this,LoginActivity::class.java)
                intent.putExtra("phone",binding.tvPhone.text.toString())
                intent.putExtra("password",binding.etPassword.text.toString())
                setResult(103,intent)
                finish()
            }
        }
    }

    override fun initView() {
        //显示保存的手机号
        binding.tvPhone.text = SPUtils.getInstance().getString("phone")
        //获取验证码
        binding.btnCode.setOnClickListener {
            viewModel.sendCode(binding.tvPhone.text.toString())
        }
        //点击保存实现修改密码
        binding.btnSave.setOnClickListener {
            viewModel.password(binding.tvPhone.text.toString(),binding.etCode.text.toString(),binding.etPassword.text.toString())
        }

    }

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_password

}